package org.example.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;


/**
 * @ Author     ：hanxi
 * @ Date       ：Created in 0:52 2024/5/18
 * @ Description：
 */
@Slf4j
public class HttpClientUtil {
    private static final HttpClient httpClient = new HttpClient();

    static {
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);
    }

    /**
     * JSoup Post请求
     * @param url 地址
     * @param params JSON参数
     * @return JSON字符串返回值
     */
    public static String doPostJSoup(String url, String params) {
        Connection.Response response = null;
        try {
            response = Jsoup.connect(url)
                    .header("content-type", "application/json;charset=utf-8")
                    .method(Connection.Method.POST)
                    .requestBody(params)
                    .ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSoup Post请求 带有 token
     * @param url 地址
     * @param tokenName token名称
     * @param token token值
     * @param params 参数
     * @return JSON字符串返回值
     */
    public static String doPostJSoup(String url,String tokenName,String token,String params){
        Connection.Response response = null;
        try {
            Connection connect = Jsoup.connect(url);
            connect.method(Connection.Method.POST);
            if(StringUtils.isNotEmpty(tokenName) && StringUtils.isNotEmpty(token)){
                connect.header(tokenName,token);
            }
            response = connect.ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JSoup Get请求
     * @param url 地址
     * @param params 参数 类型Map
     * @return JSON字符串返回值
     */
    public static String doGetJSoup(String url, Map<String,String> params){
        Connection.Response response = null;
        try {
            Connection connect = Jsoup.connect(url);
            connect.method(Connection.Method.GET);
            for(Map.Entry<String,String> entry : params.entrySet()){
                connect.data(entry.getKey(),entry.getValue());
            }
            response = connect.ignoreContentType(true)
                    .execute();
            return response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 发送POST请求
     *
     * @param url
     * @param paramJson
     * @return
     */
    public static String doPost(String url, String paramJson) {
        log.info("开始发送POST请求;url:{};参数:", url, paramJson);
        PostMethod postMethod = new PostMethod(url);
        // 设置post请求超时时间
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        postMethod.addRequestHeader("Content-Type", "application/json");
        String response = null;
        try {
            RequestEntity entity = new StringRequestEntity(paramJson, "application/json", "UTF-8");
            postMethod.setRequestEntity(entity);
            httpClient.executeMethod(postMethod);

            response = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
        } catch (IOException e) {
            log.info("POST发出请求异常,异常原因:{}", e);
            e.printStackTrace();
        }
        log.info("发送POST请求结束,返回值为:{}", response);
        return response;
    }

    /**
     * 发送GET请求
     *
     * @param urlParam
     * @return
     */
    public static String doGet(String urlParam) {
        log.info("开始发送GET请求;url:{};", urlParam);
        GetMethod getMethod = new GetMethod(urlParam);
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        getMethod.addRequestHeader("Content-Type", "application/json");
        String response = null;
        try {
            httpClient.executeMethod(getMethod);
            response = getMethod.getResponseBodyAsString();
        } catch (IOException e) {
            log.info("GET发出请求异常,异常原因:{}", e);
            e.printStackTrace();
        }
        log.info("发送GET请求结束,返回值为:{}", response);
        return response;
    }

}
